<?php
class CdeMultimediasController extends AppController {
	var $name = 'CdeMultimedias';
	var $helpers = array('Html', 'Form', 'Element');

	// Index media - List all media - DONE
	function index() {
		$this->CdeMultimedia->recursive = 0;
		$this->set('cdeMultimedias', $this->paginate());
	}

	// Create new media - DONE
	function add() {
		if (!empty($this->data)) {
			$this->CdeMultimedia->create();
			// Check if the media with the entered URL is already exist
			$existMedia = $this->CdeMultimedia->find('all', array('conditions' => array('url' => $this->data['CdeMultimedia']['url'])));
			if ($existMedia) {
				$this->Session->setFlash(__('Media with URL "'.$this->data['CdeMultimedia']['url'].'" is already exist under description"'.$existMedia[0]['CdeMultimedia']['description'].'", please enter again', true), 'default', array('class' => 'error-message'));
			} else {
				if ($this->CdeMultimedia->save($this->data)) {
					// Create new media successfully, redirect to index
					$this->Session->setFlash(__('The new media has been successfully created', true), 'default', array('class' => 'success-message'));
					$this->redirect(array('action'=>'index'));
				}
				
				// If it can reach here, error occurred, cannot create new media
				$this->Session->setFlash(__('An error occurred, new media could not be successfully created. Please try again!', true), 'default', array('class' => 'error-message'));
			}
		}
	}

	// Update media - DONE
	function edit($id = null) {
		// Check if the media ID is provided, if not display error message and redirect to index
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Media ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
		
		// Get the media by ID
		$cdeMedia = $this->CdeMultimedia->read(null, $id);
		if (!$cdeMedia && empty($this->data)) {
			// If the media is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid media ID, cannot edit', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		} else {
			if (!empty($this->data)) {
				// On update action
				// Check if the media with the URL is already exist
				$existMedia = $this->CdeMultimedia->find('all', array('conditions' => array('url' => $this->data['CdeMultimedia']['url'], 'id !=' => $this->data['CdeMultimedia']['id'])));
				if ($existMedia) {
					$this->Session->setFlash(__('Media with URL "'.$this->data['CdeMultimedia']['url'].'" is already exist under description"'.$existMedia[0]['CdeMultimedia']['description'].'", please enter again', true), 'default', array('class' => 'error-message'));
				} else {
					if ($this->CdeMultimedia->save($this->data)) {
						// Update successfully, redirect to index
						$this->Session->setFlash(__('The media has been successfully updated', true), 'default', array('class' => 'success-message'));
						$this->redirect(array('action'=>'index'));
					} else {
						// Error occurred, cannot update
						$this->Session->setFlash(__('The media could not be updated. Please try again!', true), 'default', array('class' => 'error-message'));
					}
				}
			} else {
				// Set the data to update
				$this->data = $cdeMedia;
			}
		}
	}

	// Delete media - DONE
	function delete($id = null) {
		// Check if the media ID is provided, if not display error message and redirect to index
		if (!$id) {
			$this->Session->setFlash(__('Media ID is required', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}

		// Get the media by ID
		$cdeMedia = $this->CdeMultimedia->read(null, $id);
		if ($cdeMedia != null) {
			// If the media is found, delete it
			if ($this->CdeMultimedia->del($id)) {
				// Delete successfully, redirect to index
				$this->Session->setFlash(__('The media has been successfully deleted', true), 'default', array('class' => 'success-message'));
				$this->redirect(array('action'=>'index'));
			} else {
				// Error occurred, cannot delete, redirect to index
				$this->Session->setFlash(__('An error occurred, cannot delete the media. Please try again!', true), 'default', array('class' => 'error-message'));
				$this->redirect(array('action'=>'index'));
			}
		} else {
			// If the media is not found display the error message and redirect to index
			$this->Session->setFlash(__('Invalid media ID, cannot delete', true), 'default', array('class' => 'error-message'));
			$this->redirect(array('action'=>'index'));
		}
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid CdeMultimedia.', true));
			$this->redirect(array('action'=>'index'));
		}
		
		$this->set('cdeMultimedia', $this->CdeMultimedia->read(null, $id));
	}
}
?>